import util from '../utils/util'
module.exports = Behavior({
  properties: {},
  data: {
    btn_clickable: true,
  },
  created: function () {},
  attached: function () {},
  ready: function () {},

  methods: {
    checkAll(event) {
      this.setData({
        checkedAll: event.detail,
      });
    },

    cartedit(e) {
      // if (parseInt(e.detail.detail) == 0 || parseInt(e.detail) == 0) {
      //   return
      // }
      if (this.data.btn_clickable) {
        // console.log(e)
        if (!this.data.cateAdding) {
          this.setData({
            cateAdding: true
          })
          // console.log(e)
          //判断是购物车传参还是 父组件参数
          if (e.type == "editCart") {
            var good = e.detail.currentTarget.dataset.item;
            var goods_num = parseInt(e.detail.detail);
            // console.log(this.data.shopGoods)
          } else if (e.type == "change") {
            var good = e.currentTarget.dataset.good;
            var goods_num = parseInt(e.detail);
          }
          var car_id = ''
          if (!good.car_id) {
            //如果是从商品页面修改，到购物车查找这个car_id
            for (let item of this.data.cartArr) {
              if (good.goods_id == item.goods_id) {
                car_id = item.car_id
              }
            }
          } else {
            //从购物车修改的可以直接获取到购物车id
            car_id = good.car_id
          }
          // console.log(e.detail)
          let data = {
            car_id,
            goods_num,
          }
          util.sendRequest('goods/cartedit', 'POST', data).then(res => {
            // console.log('修改购物车')
            if (res.data.code === 0) {
              this.getCart()
              //修改对应商品列表中的这个数值
              var goods_id = good.goods_id;
              if (this.data.item) {
                this.setData({
                  'item.goods_num': goods_num
                })
              } else {
                for (let i in this.data.shopGoods) {
                  // console.log(this.data.shopGoods[i].goods_id)
                  if (this.data.shopGoods[i].goods_id == goods_id) {
                    this.setData({
                      [`shopGoods[${i}].goods_num`]: goods_num,
                    })
                  }
                }
              }
            }
          })
        } else {
          // wx.showToast({
          //   title: '操作过快...',
          //   icon: 'none'
          // })
        }
      }
    },

    asyncCart(e) {
      // console.log(e)
      if (!this.data.cateAdding) {
        this.addToCart(e)
      }
    },

    deleteCart(e) {
      //购物车组件删除商品之后本地数据的处理
      this.getCart();
      // console.log(e.detail)
      let deleteArr = e.detail;
      for (let item of deleteArr) {
        if (this.data.item) {
          if (this.data.item.goods_id == item.goods_id) {
            this.setData({
              'item.goods_num': 0
            })
          }
        } else {
          for (let i in this.data.shopGoods) {
            if (this.data.shopGoods[i].goods_id == item.goods_id) {
              this.setData({
                [`shopGoods[${i}].goods_num`]: 0
              })
            }
          }
        }
      }
    },

    getCart() {
      let data = {
        page: '',
        limit: '',
        shop_id: getApp().globalData.enterShopInfo.shop_id,
      }
      util.sendRequest('goods/cartlist', 'GET', data).then(res => {
        if (res.data.code === 0) {
          var result = res.data.result;
          //判断cart物品的check状态 1.如果有全局cartData,比较并设置check的状态，2.如果没有，全部设置成true
          var cartData = getApp().globalData.cartData
          if (!cartData) {
            cartData = []
          }
          for (let itemA of result.data) {
            itemA.checked = true;
            for (let itemB of cartData) {
              // console.log(itemA.goods_id === itemB.goods_id)
              if (itemA.goods_id === itemB.goods_id) {
                itemA.checked = itemB.checked;
              }
            }
            // itemA.checked = true
          }
          // console.log('cart:', result.data)
          this.setData({
            cartArr: result.data,
            cateAdding: false
          })
          //当判断为从商品详情页返回的时候，根据购物车信息修改shopGoods数量
          // console.log(getApp().globalData.backFromDetail)
          if (getApp().globalData.backFromDetail && this.data.shopGoods) {
            // console.log('aaa')
            getApp().globalData.backFromDetail = false
            let data = this.data.shopGoods
            for (let A of data) {
              for (let B of result.data) {
                if (A.goods_id == B.goods_id) {
                  A.goods_num = B.goods_num
                }
              }
            }
            this.setData({
              shopGoods: data
            })
          }
        } else {
          console.log(res)
        }
      })
    },

    addToCart(e) {
      // console.log(e.currentTarget.dataset)
      // console.log(this.data.shopGoods)
      var goods = '';
      var index = -1;
      if (e.currentTarget.dataset.item) {
        goods = e.currentTarget.dataset.item
      } else {
        index = e.currentTarget.dataset.value;
        console.log(e.currentTarget.dataset.value)
        console.log(this.data.shopGoods)
        goods = this.data.shopGoods[index]
      }
      // console.log(goods)
      let data = {
        goods_id: goods.goods_id,
        shop_id: goods.shop_id,
        goods_num: 1
      }
      //将这个商品加入到购物车
      util.sendRequest('goods/addcart', 'POST', data).then(res => {
        // console.log('添加到购物车：', res)
        if (res.data.code === 0) {
          //修改本地显示参数
          if (!e.currentTarget.dataset.item) {
            this.setData({
              [`shopGoods[${index}].goods_num`]: 1
            })
          } else {
            this.setData({
              'item.goods_num': 1
            })
          }
          //添加打购物车成功， 获取最新的购物车信息
          this.getCart()
        }
      })
    },
  }
})